import numpy as np
from flask import Flask, jsonify, request
import ctypes

app = Flask(__name__)

# 矩阵计算服务
@app.route('/matrix', methods=['GET'])
def matrix_multiply():
    a = np.array([[1,2],[3,4]])
    b = np.array([[5,6],[7,8]])
    return jsonify({"result": np.dot(a,b).tolist()})

# 传感器数据处理
@app.route('/sensor-data', methods=['POST'])
def sensor_data():
    data = request.json['readings']
    avg = np.mean(data)
    max_val = np.max(data)
    return jsonify({"avg": avg, "max": max_val, "count": len(data)})

# 加载C字符串处理库
lib = ctypes.CDLL('./libstr.so')
lib.reverse_string.argtypes = [ctypes.c_char_p]

# 字符串反转服务
@app.route('/reverse', methods=['POST'])
def reverse_string():
    text = request.json['text'].encode('utf-8')
    buffer = ctypes.create_string_buffer(text)
    lib.reverse_string(buffer)
    return jsonify({"result": buffer.value.decode('utf-8')})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)